import gradio as gr
import os
import subprocess

code_placeholder = """#include <pthread.h>
#include <stdlib.h>

// Global shared variable
int data = 0;

void* increment(void* v) {
    for (int i = 0; i < 1000000; i++) {
        data++;
    }
    return NULL;
}

int main() {
    pthread_t t1, t2;

    // Create two concurrent threads
    pthread_create(&t1, NULL, increment, NULL);
    pthread_create(&t2, NULL, increment, NULL);
    
    // Wait for both threads to finish
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

}

"""


if __name__ == '__main__':
    if not os.path.exists("./config/config2.yaml"):
        print("Missing config2.yaml in the config folder")
        exit(-1)
    from c2rust_script import c2rust_run

    demo = gr.Interface(
        fn=c2rust_run,
        inputs=[
            gr.Textbox(value=code_placeholder),
            # "text",  
        ],
        outputs=[gr.Text(label="Rust Code"), gr.Text(label="Compiler Message")],
        description="请在下面的文本框中输入将要转译的C代码. [点击这里](http://127.0.0.1:5400) 查看实时转译过程"
    )

    process = subprocess.Popen(["python","demo_app/message_show.py"])

    demo.launch(server_name="0.0.0.0", share=False)
